import random
import csv
with open("train.csv", "w", encoding='utf-8', newline='') as csvfile:
    writer = csv.writer(csvfile)
    columns=['x','y']
    data=[]
    for i in range(20):
       x=[]
       x.append(round((random.uniform(0,5)),2))
       x.append(random.choice([0,1]))
       data.append(x)
    # 先写入columns_name
    writer.writerow(columns)
    # 写入多行用writerows
    writer.writerows(data)